package com.im.tcp.receiver.process;

import com.ecode.imecode.pack.MessagePack;
import com.im.tcp.utils.SessionSocketHolder;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class BaseProcess {


    protected abstract void processBefore();

    /**
     * 将消息投递给目标channel
     * @param messagePack
     */
    public boolean processMsg(MessagePack messagePack){
        processAfter();
        NioSocketChannel channel = SessionSocketHolder.get(messagePack.getToAppId(),
                messagePack.getToUserId(), messagePack.getToClientType(), messagePack.getToImei());
        if (channel!=null) {
            channel.writeAndFlush(messagePack);
            return true;
        }
        log.error("msg==={} 未找到投递目标channel客户端",messagePack);
        processBefore();
        return false;
    }

    protected abstract void processAfter();
}
